﻿#include "CMainWindow.h"
#include "PTZController.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QMessageBox>
#include <QFileDialog>
#include <QWidget>

// 构造函数：初始化窗口并加载界面
CMainWindow::CMainWindow(QWidget *parent)
    : CFramelessWidget(parent)
{
    initUI();          // 初始化界面控件
    resize(1600, 1200); // 设置窗口初始大小
    m_network=new NetworkManager(this);
//    _ptzController=new PTZController(this);
     _ptzController = new PTZController("http://192.168.5.222", this);
    // 绑定控制结果信号（显示操作反馈）
      connect(_ptzController, &PTZController::controlResult,
              this, [this](bool success, const QString& msg) {
          if (success) {
              qDebug() << "PTZ控制成功:" << msg;
              // 可在此处更新UI状态（如按钮状态）
          } else {
              qDebug() << "PTZ控制失败:" << msg;
              // 显示错误提示
              // QMessageBox::warning(this, "控制失败", msg);
          }
      });
}

// 关闭窗口的槽函数：响应标题栏关闭信号
void CMainWindow::closeSlot()
{
    // 弹出确认对话框
    QMessageBox::StandardButton result = QMessageBox::warning(
        this,
        "确认退出",
        "是否确定关闭程序？",
        QMessageBox::Yes | QMessageBox::No,
        QMessageBox::No
    );
    if (result == QMessageBox::Yes) {
        close(); // 确认后关闭窗口
    }
}

void CMainWindow::onOpenVideo1()
{

    QString path = "rtsp://admin:admin@192.168.5.222/live/chn=0";
    path.toStdString().c_str();
    if (!path.isEmpty()) {
        bool isPlaySuccess = _player1->playURL(path, (void*)_videoWidget1->winId());
        if (!isPlaySuccess) {
            QMessageBox::warning(this, "播放失败", "无法播放该视频文件，请检查格式或文件路径");
        }
    }
}

// 打开第二个视频：响应按钮点击
void CMainWindow::onOpenVideo2()
{
    // 逻辑与第一个播放器一致

    QString path = "http://192.168.5.100:80/hdl/hlsram/live0.flv";
    path.toStdString().c_str();
    if (!path.isEmpty()) {
        bool isPlaySuccess = _player2->playURL(path, (void*)_videoWidget2->winId());
        if (!isPlaySuccess) {
            QMessageBox::warning(this, "播放失败", "无法播放该视频文件，请检查格式或文件路径");
        }
    }
}

void CMainWindow::initUI()
{
    // ==============================================
    // 主窗口基础设置
    // ==============================================
    setStyleSheet("QMainWindow { background-color: #000000; border: none; }");
    setMinimumSize(1200, 800);
    //    setWindowState(Qt::WindowMaximized);

    // ==============================================
    // 标题栏初始化
    // ==============================================
    _ptitleBar = new CTitleBar(this);
    _ptitleBar->setMinimumHeight(40);
    _ptitleBar->setStyleSheet("background-color: #1a1a1a;");

    //创建网络连接实例
    m_network = new NetworkManager(this);
    // ==============================================
    // 视频窗口与操作区容器初始化
    // ==============================================
    // 视频窗口1
    _videoWidget1 = new QWidget(this);
    _videoWidget1->setStyleSheet("background-color: #000000; border-radius: 4px; border: 1px solid #333;");
    _videoWidget1->setMinimumSize(920, 690);

    // 视频窗口2
    _videoWidget2 = new QWidget(this);
    _videoWidget2->setStyleSheet("background-color: #000000; border-radius: 4px; border: 1px solid #333;");
    _videoWidget2->setMinimumSize(920, 690);

    // 操作区容器（深灰色背景）
    QWidget *operateWidget1 = new QWidget(this);
    operateWidget1->setStyleSheet("background-color: #1a1a1a; border-radius: 4px;");
    operateWidget1->setMinimumHeight(400);  // 增加高度容纳更大的方向键容器
    operateWidget1->setMinimumWidth(600);   // 增加宽度避免拥挤

    QWidget *operateWidget2 = new QWidget(this);
    operateWidget2->setStyleSheet("background-color: #1a1a1a; border-radius: 4px;");
    operateWidget2->setMinimumHeight(350);

    // ==============================================
    // VLC播放器初始化
    // ==============================================
    _player1 = new VLCKits(this);
    _player2 = new VLCKits(this);
    if (!_player1->initVLC()) QMessageBox::critical(this, "初始化失败", "播放器1初始化VLC引擎失败");
    if (!_player2->initVLC()) QMessageBox::critical(this, "初始化失败", "播放器2初始化VLC引擎失败");

    // ==============================================
    // 通用样式定义
    // ==============================================
    // 方向按钮样式（8方向布局）
    QString dirBtnStyle =
        "QPushButton { background-color: #2d2d2d; border-radius: 25px; border: 1px solid #444; "
        "color: white; font-size: 16px; font-weight: bold; }"
        "QPushButton:hover { background-color: #3a3a3a; }"
        "QPushButton:pressed { background-color: #1f1f1f; }";

    // 重置按钮样式（红色圆形）
    QString resetBtnStyle =
        "QPushButton { background-color: #ff9800; border-radius: 30px; border: 2px solid #f57c00; "
        "color: white; font-size: 14px; font-weight: bold; }"
        "QPushButton:hover { background-color: #fb8c00; }"
        "QPushButton:pressed { background-color: #f57c00; }";

    // 缩放按钮样式
    QString zoomBtnStyle =
        "QPushButton { background-color: #2d2d2d; border-radius: 25px; border: 1px solid #444; "
        "color: white; font-size: 24px; font-weight: bold; }"
        "QPushButton:hover { background-color: #3a3a3a; }"
        "QPushButton:pressed { background-color: #1f1f1f; }";

    // ==============================================
    // 播放器1操作区（8方向按钮+中心重置按钮）
    // ==============================================
    // 1. 打开视频按钮
    _btnOpen1 = new QPushButton("打开王道球机", operateWidget1);
    _btnOpen1->setStyleSheet(
        "QPushButton { background-color: #4CAF50; color: white; padding: 8px; "
        "border-radius: 4px; font-weight: bold; }"
        "QPushButton:hover { background-color: #45a049; }"
        "QPushButton:pressed { background-color: #3d8b40; }"
    );

    // 2. 缩放按钮部件
    QWidget *zoomWidget1 = new QWidget(operateWidget1);
    zoomWidget1->setStyleSheet("background-color: transparent;");
    QHBoxLayout *zoomLayout1 = new QHBoxLayout(zoomWidget1);
    zoomLayout1->setContentsMargins(0, 0, 0, 0);
    zoomLayout1->setSpacing(15);

    QPushButton *add1 = new QPushButton("+", zoomWidget1);
    QPushButton *sub1 = new QPushButton("-", zoomWidget1);
    add1->setFixedSize(50, 50);
    sub1->setFixedSize(50, 50);
    add1->setStyleSheet(zoomBtnStyle);
    sub1->setStyleSheet(zoomBtnStyle);
    zoomLayout1->addWidget(add1);
    zoomLayout1->addWidget(sub1);

    // 3. 8方向按钮+中心重置按钮部件（调大圆形容器）
    QWidget *dirWidget1 = new QWidget(operateWidget1);
    dirWidget1->setStyleSheet("background-color: #252525; border-radius: 140px;");  // 增大圆形半径
    dirWidget1->setFixedSize(280, 280);  // 容器调大到280x280（半径140）
    QGridLayout *dirLayout1 = new QGridLayout(dirWidget1);
    dirLayout1->setContentsMargins(40, 40, 40, 40);  // 增加内边距
    dirLayout1->setSpacing(5);

    // 8方向按钮（尺寸略小，确保能放入圆形）
    QPushButton *btnUp1 = new QPushButton("↑", dirWidget1);          // 上
    QPushButton *btnDown1 = new QPushButton("↓", dirWidget1);        // 下
    QPushButton *btnLeft1 = new QPushButton("←", dirWidget1);        // 左
    QPushButton *btnRight1 = new QPushButton("→", dirWidget1);       // 右
    QPushButton *btnUpLeft1 = new QPushButton("↖", dirWidget1);      // 左上
    QPushButton *btnUpRight1 = new QPushButton("↗", dirWidget1);     // 右上
    QPushButton *btnDownLeft1 = new QPushButton("↙", dirWidget1);    // 左下
    QPushButton *btnDownRight1 = new QPushButton("↘", dirWidget1);   // 右下
    // 中心重置按钮（红色圆形）
    QPushButton *btnReset1 = new QPushButton("停止", dirWidget1);    // 停止按钮

    // 设置按钮大小（方向按钮50x50，重置按钮稍大60x60突出显示）
    int btnSize = 50;
    btnUp1->setFixedSize(btnSize, btnSize);
    btnDown1->setFixedSize(btnSize, btnSize);
    btnLeft1->setFixedSize(btnSize, btnSize);
    btnRight1->setFixedSize(btnSize, btnSize);
    btnUpLeft1->setFixedSize(btnSize, btnSize);
    btnUpRight1->setFixedSize(btnSize, btnSize);
    btnDownLeft1->setFixedSize(btnSize, btnSize);
    btnDownRight1->setFixedSize(btnSize, btnSize);
    btnReset1->setFixedSize(60, 60);  // 重置按钮稍大

    // 应用样式
    btnUp1->setStyleSheet(dirBtnStyle);
    btnDown1->setStyleSheet(dirBtnStyle);
    btnLeft1->setStyleSheet(dirBtnStyle);
    btnRight1->setStyleSheet(dirBtnStyle);
    btnUpLeft1->setStyleSheet(dirBtnStyle);
    btnUpRight1->setStyleSheet(dirBtnStyle);
    btnDownLeft1->setStyleSheet(dirBtnStyle);
    btnDownRight1->setStyleSheet(dirBtnStyle);
    btnReset1->setStyleSheet(resetBtnStyle);  // 应用红色样式

    // 9按钮网格布局（3x3网格，中心为重置按钮）
    dirLayout1->addWidget(btnUpLeft1, 0, 0, Qt::AlignCenter);    // 左上
    dirLayout1->addWidget(btnUp1, 0, 1, Qt::AlignCenter);        // 上
    dirLayout1->addWidget(btnUpRight1, 0, 2, Qt::AlignCenter);   // 右上
    dirLayout1->addWidget(btnLeft1, 1, 0, Qt::AlignCenter);      // 左
    dirLayout1->addWidget(btnReset1, 1, 1, Qt::AlignCenter);     // 中心重置按钮
    dirLayout1->addWidget(btnRight1, 1, 2, Qt::AlignCenter);     // 右
    dirLayout1->addWidget(btnDownLeft1, 2, 0, Qt::AlignCenter);  // 左下
    dirLayout1->addWidget(btnDown1, 2, 1, Qt::AlignCenter);      // 下
    dirLayout1->addWidget(btnDownRight1, 2, 2, Qt::AlignCenter); // 右下

    // 4. 操作区1主布局
    QWidget *operateContent1 = new QWidget(operateWidget1);
    operateContent1->setStyleSheet("background-color: transparent;");
    QHBoxLayout *operateMainLayout1 = new QHBoxLayout(operateContent1);
    operateMainLayout1->setContentsMargins(0, 0, 0, 0);
    operateMainLayout1->setSpacing(30);  // 缩放与方向部件间距

    operateMainLayout1->addWidget(zoomWidget1);
    operateMainLayout1->addWidget(dirWidget1);
    operateMainLayout1->addStretch();

    // 5. 操作区1整体布局
    QVBoxLayout *operateLayout1 = new QVBoxLayout(operateWidget1);
    operateLayout1->addWidget(_btnOpen1);
    operateLayout1->addSpacing(15);
    operateLayout1->addWidget(operateContent1);
    operateLayout1->setContentsMargins(15, 15, 15, 15);
    operateLayout1->setAlignment(operateContent1, Qt::AlignLeft | Qt::AlignTop);

    // ==============================================
    // 播放器2操作区
    // ==============================================
    // 1. 打开视频按钮
    _btnOpen2 = new QPushButton("打开王道枪机", operateWidget2);
    _btnOpen2->setStyleSheet(
        "QPushButton { background-color: #2196F3; color: white; padding: 8px; "
        "border-radius: 4px; font-weight: bold; }"
        "QPushButton:hover { background-color: #0b7dda; }"
        "QPushButton:pressed { background-color: #0a69b7; }"
    );

    // 2. 缩放按钮部件
    QWidget *zoomWidget2 = new QWidget(operateWidget2);
    zoomWidget2->setStyleSheet("background-color: transparent;");
    QHBoxLayout *zoomLayout2 = new QHBoxLayout(zoomWidget2);
    zoomLayout2->setContentsMargins(0, 0, 0, 0);
    zoomLayout2->setSpacing(15);

    QPushButton *add2 = new QPushButton("+", zoomWidget2);
    QPushButton *sub2 = new QPushButton("-", zoomWidget2);
    add2->setFixedSize(50, 50);
    sub2->setFixedSize(50, 50);
    add2->setStyleSheet(zoomBtnStyle);
    sub2->setStyleSheet(zoomBtnStyle);
    zoomLayout2->addWidget(add2);
    zoomLayout2->addWidget(sub2);
    zoomLayout2->addStretch();

    // 3. 操作区2整体布局
    QVBoxLayout *operateLayout2 = new QVBoxLayout(operateWidget2);
    operateLayout2->addWidget(_btnOpen2);
    operateLayout2->addSpacing(15);
    operateLayout2->addWidget(zoomWidget2);
    operateLayout2->addStretch();
    operateLayout2->setContentsMargins(15, 15, 15, 15);
    operateLayout2->setAlignment(zoomWidget2, Qt::AlignLeft | Qt::AlignTop);

    // ==============================================
    // 信号绑定（新增重置按钮信号）
    // ==============================================
    connect(_btnOpen1, &QPushButton::clicked, this, &CMainWindow::onOpenVideo1);
    connect(_btnOpen2, &QPushButton::clicked, this, &CMainWindow::onOpenVideo2);
//    connect(btnUp1, &QPushButton::clicked, this, &CMainWindow::onCamera1Up);
//    connect(btnDown1, &QPushButton::clicked, this, &CMainWindow::onCamera1Down);
//    connect(btnLeft1, &QPushButton::clicked, this, &CMainWindow::onCamera1left);
//    connect(btnRight1, &QPushButton::clicked, this, &CMainWindow::onCamera1Right);
//    connect(btnUpLeft1, &QPushButton::clicked, this, &CMainWindow::onCamera1Upleft);      // 新增
//    connect(btnUpRight1, &QPushButton::clicked, this, &CMainWindow::onCamera1UpRight);    // 新增
//    connect(btnDownLeft1, &QPushButton::clicked, this, &CMainWindow::onCamera1Downleft);  // 新增
//    connect(btnDownRight1, &QPushButton::clicked, this, &CMainWindow::onCamera1DownRight);// 新增
//    connect(btnReset1, &QPushButton::clicked, this, &CMainWindow::onCamera1Reset);        // 新增重置信号
//    connect(add1, &QPushButton::clicked, this, &CMainWindow::onCamera1ZoomIn);
//    connect(sub1, &QPushButton::clicked, this, &CMainWindow::onCamera1ZoomOut);
//    connect(add2, &QPushButton::clicked, this, &CMainWindow::onCamera2ZoomIn);
//    connect(sub2, &QPushButton::clicked, this, &CMainWindow::onCamera2ZoomOut);
    connect(btnUp1, &QPushButton::clicked,
                this, [this](){ _ptzController->sendControlCommand("u", 50); });         // 上
    connect(btnDown1, &QPushButton::clicked,
                this, [this](){ _ptzController->sendControlCommand("d", 50); });       // 下
    connect(btnLeft1, &QPushButton::clicked,
                this, [this](){ _ptzController->sendControlCommand("l", 50); });       // 左
    connect(btnRight1, &QPushButton::clicked,
                this, [this](){ _ptzController->sendControlCommand("r", 50); });      // 右
    connect(btnUpLeft1, &QPushButton::clicked,
                this, [this](){ _ptzController->sendControlCommand("1", 50); });         // 左上
    connect(btnUpRight1, &QPushButton::clicked,
                this, [this](){ _ptzController->sendControlCommand("2", 50); });       // 右上
    connect(btnDownLeft1, &QPushButton::clicked,
                this, [this](){ _ptzController->sendControlCommand("3", 50); });       // 左下
    connect(btnDownRight1, &QPushButton::clicked,
                this, [this](){ _ptzController->sendControlCommand("r", 50); });      // 右下
    connect(btnReset1, &QPushButton::clicked,
                this, [this](){ _ptzController->sendControlCommand("s", 50); });       // 停止转动


    // ==============================================
    // 整体布局组装
    // ==============================================
    // 视频窗口布局
    QWidget *videoFrame = new QWidget(this);
    videoFrame->setStyleSheet("background-color: #000000;");
    QHBoxLayout *videoLayout = new QHBoxLayout(videoFrame);
    videoLayout->addWidget(_videoWidget1);
    videoLayout->addWidget(_videoWidget2);
    videoLayout->setSpacing(0);
    videoLayout->setContentsMargins(0, 0, 0, 0);

    // 操作区布局
    QWidget *operateFrame = new QWidget(this);
    operateFrame->setStyleSheet("background-color: #000000;");
    QHBoxLayout *operateLayout = new QHBoxLayout(operateFrame);
    operateLayout->addWidget(operateWidget1);
    operateLayout->addWidget(operateWidget2);
    operateLayout->setSpacing(10);
    operateLayout->setContentsMargins(5, 5, 5, 5);

    // 主内容布局
    QWidget *mainContent = new QWidget(this);
    mainContent->setStyleSheet("background-color: #000000;");
    QVBoxLayout *mainContentLayout = new QVBoxLayout(mainContent);
    mainContentLayout->addWidget(videoFrame);
    mainContentLayout->addWidget(operateFrame);
    mainContentLayout->setSpacing(5);
    mainContentLayout->setContentsMargins(0, 0, 0, 0);

    // 主窗口布局
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->addWidget(_ptitleBar);
    mainLayout->addWidget(mainContent);
    mainLayout->setContentsMargins(0, 0, 0, 0);
    mainLayout->setSpacing(0);

    // 标题栏关闭信号
    connect(_ptitleBar, &CTitleBar::sigClose, this, &CMainWindow::closeSlot);

    resize(1400, 950);  // 适当增大窗口高度
}
